<template>
	<view class="login-container">
		<view class="login-form">
			<text class="title">news注册</text>
			<view class="input-item">
				<input type="text" placeholder="请输入用户名" class="input" 	:value="username"
					@input="username = $event.detail.value"/>
			</view>
			
			<view class="input-item">
				<input type="password" placeholder="请输入密码" class="input" 	:value="password"
					@input="password = $event.detail.value"/>
			</view>
			<view class="input-item">
				<input type="password" placeholder="请确认密码" class="input" :value="confirmPassword"
					@input="confirmPassword = $event.detail.value"/>
			</view>
			
			<view class="login-btn">
				<button @click="handleStore">注册</button>
			</view>
			
			<view  class="register-link">
				<text>有账号？</text>
				<view @click="goLogin" class="link">登录</view>
			</view>
			
		</view>
	</view>
	
</template>
<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: ''
			}
		},
		methods: {
			handleStore(){
				if (!this.username.trim()) {
									uni.showToast({
										title: "请输入用户名",
										icon: 'none'
									})
									return
								}
				if(this.password !== this.confirmPassword){
					uni.showToast({
						title:"俩次密码不一致",
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title:"注册中..."
				})
				console.log("点击注册，准备调用接口发送");
				uniCloud.callFunction({
					name:'addUser',
					
					data:{
						'name':this.username,
						'password': this.password
					},
					success: (mag) => {
						console.log("addUser CallBacksuccess");
						
						//Ui update
						setTimeout(() => {
							uni.hideLoading()
							uni.showToast({
								title:'注册成功',
								icon:'success'
							})
						},1000)
						setTimeout(() => {
							uni.navigateBack();
						},1500)
						
					},fail: (res) => {
						console.warn("addUser callBackfain");
					}
				})
			},
			goLogin(){
				uni.reLaunch({
					url:"/pages/newsTabBar/newsLogin/newsLogin"
				})
			}
			
		}
	}
</script>

<style>

	.login-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		min-height: 100vh;
		background-color:#f5f5f5;
	}
	.login-form{
		display: flex;
		flex-direction: column;
		width: 80%;
		max-width: 400px;
		padding: 20px;
		background-color:#fff;
		border-radius: 8px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
	}
	.title{
		font-size: 20px;
		font-weight: bold;
		text-align: center;
		margin-bottom: 20px;
	}
	.input-item{
		margin-bottom: 15px;
	}
	.input{
		padding-left: 5px;
		width: 100%;
		height: 40px;
		border: 1px solid #ddd;
		border-radius: 4px;
		box-sizing: border-box;
	}
	.login-btn{
		border-radius: 4px;
		padding: 1px;
		margin-bottom: 15px;
	}
	.register-link{
		display: flex;
		justify-content: center;
		font-size: 14px;
	}
	.link{
		color: #007aff;
		margin-left: 5px;
		
	}
</style>
